home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tspa2560.zip / TSUNTB.INT < prev    next >
Text File  |  1991-10-27  |  7KB  |  169 lines

  1. {$B-,D-,F-,I+,N-,R-,S+,V+}
  2.  
  3. (*
  4. Timo Salmi UNiT B
  5. A Turbo Pascal unit for (a) bit (of) manipulation
  6. All rights reserved 22-Jul-89,
  7. Updated 26-Jul-89, 19-Aug-89, 18-Oct-89, 17-Jul-90, 4-Jan-91, 27-Oct-91
  8.  
  9. Here are some power functions, bit manipulation and base conversions.
  10. There is nothing really novel about them, just that I have needed them
  11. myself, and thought to make some available to [a/be]muse the general public.
  12. Some of the functions are, however, hopefully both smaller faster than the
  13. corresponding routines in average Turbo Pascal guides, or have features
  14. that are normally lacking.
  15.  
  16. This unit may be used and distributed freely for PRIVATE, NON-COMMERCIAL,
  17. NON-INSTITUTIONAL purposes, provided it is not changed in any way. For
  18. ANY other usage, such as use in a business enterprise or at a university,
  19. contact the author for registration.
  20.  
  21. The units are under development. Comments and contacts are solicited. If
  22. you have any questions, please do not hesitate to use electronic mail for
  23. communication.
  24. InterNet address: ts@chyde.uwasa.fi         (preferred)
  25. Funet address:    GADO::SALMI
  26. Bitnet address:   SALMI@FINFUN
  27.  
  28. The author shall not be liable to the user for any direct, indirect or
  29. consequential loss arising from the use of, or inability to use, any unit,
  30. program or file howsoever caused. No warranty is given that the units and
  31. programs will work under all circumstances.
  32.  
  33. Timo Salmi
  34. Professor of Accounting and Business Finance
  35. School of Business Studies, University of Vaasa
  36. P.O. BOX 297, SF-65101 Vaasa, Finland
  37.  
  38. The following changes were made 4-Jan-91:
  39.  POWERGFN function has been rewritten
  40.  DECBINFN function has been omitted
  41.  DECHEXFN function has been omitted
  42.  
  43. The following routines were added 27-Oct-91:
  44.  BTEWRDFN
  45.  WRDLNGFN
  46.  HIWORDFN
  47.  LOWORDFN
  48. *)
  49.  
  50. unit TSUNTB;
  51.  
  52. (* ======================================================================= *)
  53.                           interface
  54. (* ======================================================================= *)
  55.  
  56. uses Dos;
  57.  
  58. (* =======================================================================
  59.                        Timer routines
  60.    ======================================================================= *)
  61.  
  62. (*
  63. If one wants to test and compare speeds of various procedures and functions
  64. one needs a procedure to give the elapsed time. TIMERFN does that. It is
  65. very simple. It just gives the seconds elapsed since midnight to the apparent
  66. precision of a hundredth of a second.
  67. *)
  68.  
  69. (* Time (seconds) elapsed since midnight *)
  70. function TIMERFN : real;
  71.  
  72. (* The timer is actually updated 18.2 times per seconds by the 8284
  73.    oscillator at 1193180 Hetz frequency. These updates are called ticks.
  74.    Ticks are calculated from midnight, and can be used as an alternative
  75.    timing. *)
  76. function TICKSFN : longint;
  77.  
  78. (*
  79. Pascal lacks a power function and therefore one has to build it oneself.
  80. The simplest way to calculate a power function is Exp(exponent*Ln(number)),
  81. which many guides suggest. It is unsatisfactory, however, from the point
  82. of view that powers of zero or negative numbers cause an error. The
  83. POWERGFN does not have this hitch, but is, of course, slightly slower.
  84. Invalid operations such as -0.55 to -3.5 are detected and running halted.
  85. In special cases a power function can be made very fast. TWOTOFN is such a
  86. function raising two to a power.
  87. *)
  88.  
  89. (* =======================================================================
  90.            Mathematical routines for raising to powers
  91.    ======================================================================= *)
  92.  
  93. (* Raise a positive number to a power the traditional way *)
  94. function POWERFN (number, exponent : real) : real;
  95.  
  96. (* Raise any number to a power, the generalized power function.
  97.    Invalid expressions and overflows are trapped properly *)
  98. function POWERGFN (number, exponent : real) : real;
  99.  
  100. (* Raise a longint to a power, fast; Do not use negative exponents *)
  101. function POWERLFN (number, exponent : longint) : longint;
  102.  
  103. (* Raise two to a power, that is 2^exponent, very fast *)
  104. function TWOTOFN (exponent : word) : word;
  105.  
  106. (* Raise sixteen to a power, that is 16^exponent, very fast *)
  107. function R16TOFN (exponent : word) : word;
  108.  
  109. (* =======================================================================
  110.              Mathematical number base conversion routines
  111.    ======================================================================= *)
  112.  
  113. (*
  114. Base conversion is a quite commonly occurring task in programming. It
  115. involves a similar problem to the one explained in discussing the
  116. routines for raising a number to a power: The routines can be made
  117. general, or they can be made fast. Here are some of the routines.
  118. *)
  119.  
  120. (* Convert a number from any base to any base (2-36),
  121.    The result may not execeed the range of a longint *)
  122. function CONVBFN (number : string; frombase, tobase : byte) : string;
  123.  
  124. (* Convert a binary string fast to a decimal word *)
  125. function BINDECFN (binary : string) : word;
  126.  
  127. (* Convert a decimal word to a binary string fast *)
  128. function BINFN (decimal : word) : string;
  129.  
  130. (* Convert a decimal longint to a binary string fast *)
  131. function LBINFN (decimal : longint) : string;
  132.  
  133. (* Convert a hexadecimal string fast to a decimal word *)
  134. function HEXDECFN (hexadecimal : string) : word;
  135.  
  136. (* Convert a decimal word to a hexadecimal string very fast *)
  137. function HEXFN (decimal : word) : string;
  138.  
  139. (* Convert a decimal longint to a hexadecimal string fast *)
  140. function LHEXFN (decimal : longint) : string;
  141.  
  142. (* =======================================================================
  143.        Bit, byte, word, and longint datatype manipulation routines
  144.    ======================================================================= *)
  145.  
  146. (*
  147. The next function is specialized, but occasionally very useful. As is
  148. known a word is made up of 16 bits numbered from 0 to 15. The following
  149. function establishes whether a particular bit is on or off in a word
  150. *)
  151. (* Is an individual bit on in a word, bits are numbered from 0-15 as usual *)
  152. function BITONFN (status : word; bit : byte) : boolean;
  153.  
  154. (* Combines two bytes into a single word.
  155.    This is the inverse of the inbuilt Hi and Lo byte functions *)
  156. function BTEWRDFN (high, low : byte) : word;
  157.  
  158. (* Combines two words into a longint *)
  159. function WRDLNGFN (high, low : word) : longint;
  160.  
  161. (* Returns the high-order word of the longint argument
  162.    Similar to Hi, but returns a word instead of a byte *)
  163. function HIWORDFN (x : longint) : word;
  164.  
  165. (* Returns the low-order word of the longint argument
  166.    Similar to Lo, but returns a word instead of a byte *)
  167. function LOWORDFN (x : longint) : word;
  168.  
  169.